package com.yy.controller.admin;

import com.yy.controller.service.DishService;
import com.yy.dto.DishPageQueryDTO;
import com.yy.entity.Dish;
import com.yy.entity.SetmealDish;
import com.yy.result.PageResult;
import com.yy.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 分页条件查询菜品列表
     *
     * @return
     */
    @GetMapping("/list")
    @CrossOrigin
    @ApiOperation("分页条件查询菜品列表")
    public Result<PageResult> list(DishPageQueryDTO dishPageQueryDTO) {
        PageResult dish = dishService.pageList(dishPageQueryDTO);
        return Result.success(dish);
    }

    /**
     * 新增菜品
     */
    @PostMapping("/add")
    @CrossOrigin
    @ApiOperation("新增菜品")
    public Result add(@RequestBody Dish dish) {
        log.info("新增菜品:{}", dish);
        dishService.add(dish);
        return Result.success();
    }

    /**
     * 修改菜品
     * @param dish
     * @return
     */
    @PutMapping("/update")
    @CrossOrigin
    @ApiOperation("修改菜品")
    public Result update(@RequestBody Dish dish) {
        log.info("修改菜品:{}",dish);
        dishService.update(dish);
        return Result.success();
    }

    /**
     * 根据id删除菜品
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @CrossOrigin
    @ApiOperation("删除菜品")
    public Result delete(@PathVariable("id") Long id) {
        log.info("删除菜品:{}",id);
        dishService.delete(id);
        return Result.success();
    }

    /**
     * 获取所有菜品
     * @return
     */
    @GetMapping("/getAllDish")
    @CrossOrigin
    @ApiOperation("查询所有菜品")
    public  Result getAllDish(){
        List<Dish> dishList = dishService.getAllDish();
        return Result.success(dishList);
    }
    @GetMapping("/getDishByIds")
    @CrossOrigin
    @ApiOperation("根据ids数组查询")
    public Result<List<SetmealDish>> getDishByIds(@RequestParam("ids") String ids){
      List<SetmealDish> setmealDishList=dishService.getDishByIds(ids);
      return  Result.success(setmealDishList);
    }

}
